<?php
/*
  $Id: new_products.php,v 1.34 2003/06/09 22:49:58 hpdl Exp $
$lastedit: V1.1.8 $
*/
$box_show_option = NEW_PRODUCTS_OLD_SHOW; 
$days = NEW_PRODUCTS_OLD_DAYS;  
$no_new_text = NEW_PRODUCTS_OLD_TEXT; 

  if(!tep_session_is_registered('sppc_customer_group_id')) { 
  $customer_group_id = '0';
  } else {
   $customer_group_id = $sppc_customer_group_id;
  }

	if ( (!isset($new_products_category_id)) || ($new_products_category_id == '0') ) {
	
		$new_products_query = tep_db_query("select distinct p.products_id, p.products_image, p.products_tax_class_id, p.products_price as products_price, pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd left join " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c using(products_id) left join " . TABLE_CATEGORIES . " c using(categories_id) where p.products_id = pd.products_id and products_status = '1' and find_in_set('" . $customer_group_id . "', products_hide_from_groups) = 0 and find_in_set('" . $customer_group_id . "', categories_hide_from_groups) = 0 and pd.language_id = '" . (int)$languages_id . "' order by p.products_date_added desc limit " . MAX_DISPLAY_NEW_PRODUCTS); 
		$check_products_query = tep_db_query("select count(products_id) as count from " . TABLE_PRODUCTS . " where products_status = '1' and products_date_added > SUBDATE( now(  ) ,  INTERVAL " . $days . "  DAY  )");
  	} else {
  
    	$new_products_query = tep_db_query("select distinct p.products_id, p.products_image, p.products_tax_class_id, p.products_price as products_price, pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd left join " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c using(products_id) left join " . TABLE_CATEGORIES . " c using(categories_id) where p.products_id = pd.products_id and c.parent_id = '" . (int)$new_products_category_id . "' and p.products_status = '1' and find_in_set('" . $customer_group_id . "', products_hide_from_groups) = 0 and find_in_set('" . $customer_group_id . "', categories_hide_from_groups) = 0 and pd.language_id = '" . (int)$languages_id . "' order by p.products_date_added desc limit ". MAX_DISPLAY_NEW_PRODUCTS);
    	$check_products_query = tep_db_query("select count(p.products_id) as count from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and c.parent_id = '" . $new_products_category_id . "' and p.products_status = '1' and products_date_added > SUBDATE( now(  ) ,  INTERVAL " . $days . "  DAY  ) limit " . MAX_DISPLAY_NEW_PRODUCTS);
  	}	
$check = tep_db_fetch_array($check_products_query);
if ($check['count'] <= 0 && $box_show_option == 1) {
echo "
<!-- new_products //-->
<!-- new_products_disabled //-->
<!-- new_products_eof //-->";
 } elseif ($check['count'] <= 0 && $box_show_option == 2) {
 $info_box_contents = array();
  $info_box_contents[] = array('text' => sprintf(TABLE_HEADING_NEW_PRODUCTS, strftime('%B')));
$a['defnprodhead'] = sprintf(TABLE_HEADING_NEW_PRODUCTS, strftime('%B'));
  //new contentBoxHeading($info_box_contents);  
?>
<table border="0" width="100%" cellspacing="0" cellpadding="2">
          <tr><td class=componentheading>
<?php
Echo $a[defnprodhead];
?></td></tr></table><?php  
  $row = 0;
  $col = 0;
  $info_box_contents = array();
    $new_products['products_name'] = tep_get_products_name($new_products['products_id']);
   
    $info_box_contents[$row][$col] = array('align' => 'left',
                                           'params' => 'class="smallText" valign="top"',
                                           'text' => $no_new_text);
 new contentBox($info_box_contents);
// require(WS_TEMPLATES . E_TEMPLATE_FOLDER . '/new_products.html');

} else {
  $info_box_contents = array();
  $info_box_contents[] = array('text' => sprintf(TABLE_HEADING_NEW_PRODUCTS, strftime('%B')));

  //new contentBoxHeading($info_box_contents);
 $a['defnprodhead'] = sprintf(TABLE_HEADING_NEW_PRODUCTS, strftime('%B'));
// global variable (session) $sppc_customer_group_id -> local variable customer_group_id
?>
<table border="0" width="100%" cellspacing="0" cellpadding="2">
          <tr><td class=componentheading>
<?php
Echo $a[defnprodhead];
?></td></tr></table><?php

 
  if (($no_of_new_products = tep_db_num_rows($new_products_query)) > 0) {
	  while ($_new_products = tep_db_fetch_array($new_products_query)) {
	$new_products[] = $_new_products;
	$list_of_prdct_ids[] = $_new_products['products_id'];
	} 

$select_list_of_prdct_ids = "products_id = '".$list_of_prdct_ids[0]."' ";
  if ($no_of_new_products > 1) {
   for ($n = 1 ; $n < count($list_of_prdct_ids) ; $n++) {
   $select_list_of_prdct_ids .= "or products_id = '".$list_of_prdct_ids[$n]."' ";   
   }
}
// get all customers_group_prices for products with the particular customer_group_id
// however not necessary for customer_group_id = 0
if ($customer_group_id != '0') {
  $pg_query = tep_db_query("select pg.products_id, customers_group_price as price from " . TABLE_PRODUCTS_GROUPS . " pg where (".$select_list_of_prdct_ids.") and pg.customers_group_id = '".$customer_group_id."'");
	while ($pg_array = tep_db_fetch_array($pg_query)) {
	$new_prices[] = array ('products_id' => $pg_array['products_id'], 'products_price' => $pg_array['price'], 'specials_new_products_price' => '');
	}

   for ($x = 0; $x < $no_of_new_products; $x++) {
// replace products prices with those from customers_group table
        if(!empty($new_prices)) {
	    for ($i = 0; $i < count($new_prices); $i++) {
		    if( $new_products[$x]['products_id'] == $new_prices[$i]['products_id'] ) {
			$new_products[$x]['products_price'] = $new_prices[$i]['products_price'];
		    }
	    }
	} // end if(!empty($new_prices)
   } // end for ($x = 0; $x < $no_of_products_new; $x++)
} // end if ($customer_group_id != '0')

// an extra query is needed for all the specials
	$specials_query = tep_db_query("select products_id, specials_new_products_price from ".TABLE_SPECIALS." where (".$select_list_of_prdct_ids.") and status = '1' and customers_group_id = '" .$customer_group_id. "' ");
	while ($specials_array = tep_db_fetch_array($specials_query)) {
	$new_s_prices[] = array ('products_id' => $specials_array['products_id'], 'specials_new_products_price' => $specials_array['specials_new_products_price']);
	}

// replace products_price with the correct specials_new_products_price
if(!empty($new_s_prices)) {
	for ($x = 0; $x < $no_of_new_products; $x++) { 
	    for ($i = 0; $i < count($new_s_prices); $i++) {
		    if( $new_products[$x]['products_id'] == $new_s_prices[$i]['products_id'] ) {
			$new_products[$x]['products_price'] = $new_s_prices[$i]['specials_new_products_price'];
		    }
	       }
	   } 
	} // // end if(!empty($new_s_prices)

  $row = 0;
  $col = 0;
   
  $mod_box_contents = array();

//  while ($new_products = tep_db_fetch_array($new_products_query)) {
	for ($x = 0; $x < $no_of_new_products; $x++) {
    //$new_products[$x]['products_name'] = tep_get_products_name($new_products[$x]['products_id']);
    
    $showprice = $currencies->display_price($new_products[$x]['products_price'], tep_get_tax_rate($new_products[$x]['products_tax_class_id']));
    
    $auction_price = tep_get_products_auction_price($new_products[$x]['products_id']);
			
			if (!$auction_price) {
   $pf->loadProductSppc((int)$new_products[$x]['products_id'], (int)$languages_id, $new_products[$x]);
   //$products_price = $pf->getPriceString();
   //$products_price2 = $pf->getPriceStringShort();
   $showprice = $pf->getPriceStringShort();
   }else{
   
   $products_price	= $currencies->display_price($auction_price, 0);
						
				$sql_auction = 'select auctions_id from '.TABLE_AUCTIONS_PRODUCTS." where products_id='".$new_products[$x]['products_id']."'";
				$auction_info = tep_db_fetch_array(tep_db_query($sql_auction));
				$auction_id = $auction_info['auctions_id'];
				
				$databid = tep_db_query("select MAX(bid_price) as bid_price from ".TABLE_AUCTIONS_BIDS.' ab,'.TABLE_AUCTIONS_PRODUCTS.' ap where ab.auctions_id=ap.auctions_id and ab.auctions_id='.$auction_id.'');
				
				while($highbid = tep_db_fetch_array( $databid ))
if ($highbid['bid_price'] > 0){
$products_price	= $currencies->display_price($highbid['bid_price'],0);
}
   
  $showprice  = TEXT_CURRENT_BID . $products_price;
   
   }
    
$sql = 'SELECT `products_description` FROM `'.TABLE_PRODUCTS_DESCRIPTION.'` WHERE products_id ='.$new_products[$x]['products_id'].' && language_id='.(int)$languages_id;
	$description_query = tep_db_query($sql);
	$description = mysql_fetch_array($description_query, MYSQL_ASSOC);        
	$description['products_description'] = substr($description['products_description'], 0, SHORT_DISC_IN_PROD2);
	$desc_len = strlen($description['products_description']);
	
	//echo $desc_len;
//$description['products_description'][$desc_len-1] = '.';
//	$description['products_description'][$desc_len-2] = '.';
//	$description['products_description'][$desc_len-3] = '.';
	$description['products_description'] .= ' ...';
	if(SHORT_DISC_IN_PROD2 > 0) {
	
    $mod_box_contents[$row][$col] = array('align' => 'center',
                                           'params' => 'class="smallText" width="33%" valign="top"',
                                      

 'text' => '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products[$x]['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $new_products[$x]['products_image'], $new_products[$x]['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products[$x]['products_id']) . '"><b>' . $new_products[$x]['products_name'] . '</b></a><br><br>' . strip_tags($description['products_description']) . '<br><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, "products_id=" . $new_products[$x]["products_id"]). '">' . DESCR_READ_MORE . '</a><br>&nbsp;' . $showprice . '<br><br>');
 }else{
  $mod_box_contents[$row][$col] = array('align' => 'center',
                                           'params' => 'class="smallText" width="33%" valign="top"',
                                      

 'text' => '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products[$x]['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $new_products[$x]['products_image'], $new_products[$x]['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products[$x]['products_id']) . '"><b>' . $new_products[$x]['products_name'] . '</b></a><br>&nbsp;' . $showprice . '<br><br>');
 }

    $col ++;
    if ($col > 2) {
      $col = 0;
      $row ++;
    }
   } // end for ($x = 0; $x < $no_of_new_products; $x++)
 } //  end if (($no_of_new_products = tep_db_num_rows($new_products_query)) > 0)
 // EOF Separate Pricing per Customer
 new contentBox($mod_box_contents);
// require(WS_TEMPLATES . E_TEMPLATE_FOLDER . '/new_products.html');
 }//end of switch
 
 
?>